﻿#region Copyright

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region Imports

using System.Collections.Generic;
using Majesty_of_Omega.Config;
using Majesty_of_Omega.Model.Game;
using Majesty_of_Omega.Model.Report;

#endregion

namespace Majesty_of_Omega.Logic.Calculate
{
    /// <summary>
    /// This class calculates the next turn with all the game play mechanics
    /// </summary>
    public class NextTurnCalculator
    {
        /// <summary>
        /// The game calculators represent the ai and calculations for several aspects of the game.
        /// </summary>
        /// <remarks>
        /// The entry position in the list represents the priority. 
        /// </remarks>
        readonly List<GameCalculator> gameCalculators = new List<GameCalculator>
                {
                 new ResourceCalculator(), 
                 new AppliedResearchProjectCalculator(),
                 new AvailableResearchCalculator(),
                 new FundamentalResearchCalculator(), 
                 new PlayerAICalculator()
                };


        /// <summary>
        /// The game state
        /// </summary>
        private readonly SaveGame saveGame;

        /// <summary>
        /// Constructs the calculator with the game
        /// </summary>
        public NextTurnCalculator(SaveGame game)
        {
            saveGame = game;
        }

        /// <summary>
        /// Calculate the next turn for the game
        /// </summary>
        public void CalculateNextTurn()
        {
            saveGame.IncreaseTurn();
            foreach (var player in saveGame.playerList)
            {
                CalculatePlayerTurn(player);
            }
        }

        /// <summary>
        /// Calculates the players next turn.
        /// </summary>
        /// <param name="player">The player.</param>
        private void CalculatePlayerTurn(Player player)
        {
            SitReportTurn reportTurn = new SitReportTurn { TurnNumber = saveGame.TurnNumber };
            CalculateInfo info = new CalculateInfo(saveGame, player, reportTurn);
            foreach (var calculator in gameCalculators)
            {
                calculator.Calculate(info);
            }
            reportTurn.AddSitRepEntry(SitReportType.TurnCompleted, null, "value1", saveGame.TurnNumber);
            player.SitReport.AddSitReportTurn(reportTurn);
        }
    }
}